<?php
class CollectionsController extends AppController
{
	public $name = "Collections";
	public $helpers = array('xml');
	public $components = array('RequestHandler');
	public $layout = 'xml/default';
	
	function beforeRender()
	{
		$this->disableCache();
	}
	
	function index()
	{
		$collections = $this->Collection->findAll();
		$this->set('collections', array('collections' => $collections));
		$this->render('index');
	}
	
	function add()
	{
		if (!$this->RequestHandler->isPost())
		{
			return;
		}
		$this->Collection->create();
		$this->Collection->set("title", $this->params['form']['title']);
		$this->Collection->set("description", $this->params['form']['description']);
		$this->Collection->set("ids", $this->params['form']['ids']); 
		
		if ($this->Collection->validates())
		{
			$this->Collection->save();
			$this->index();
		}
		else
		{
			$this->cakeError('notValidates');
		}
	}
	
	function edit()
	{
		if (!$this->RequestHandler->isPost())
		{
			return;
		}
		$this->Collection->read(null, $this->params['form']['id']);
		$this->Collection->set("title", $this->params['form']['title']);
		$this->Collection->set("description", $this->params['form']['description']);
		$this->Collection->set("ids", $this->params['form']['ids']);
		if ($this->Collection->validates())
		{
			$this->Collection->save();
			$this->index();
		}
		else
		{
			$this->cakeError('notValidates');
		}
	}
	
	function delete()
	{
		if (!$this->RequestHandler->isPost())
		{
			return;
		}
		$this->Collection->delete($this->params['form']['id']);
		$this->index();
	}
}
?>